package com.karson.admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.karson.common.model.AlbumAuthor;
import com.karson.common.model.Author;
import com.karson.common.request.AuthorUpdateRequest;
import com.karson.common.request.PageParamRequest;
import com.karson.common.response.AuthorResponse;
import com.karson.common.response.CommonResult;
import com.karson.common.vo.AuthorTempVo;
import com.karson.service.dao.AuthorMapper;
import com.karson.service.service.AlbumAuthorService;
import com.karson.service.service.AuthorService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/api/author")
@Tag(name = "作者管理接口")
public class AuthorController {

    @Autowired
    private AuthorService authorService;
    @Autowired
    private AlbumAuthorService albumAuthorService;
    @Autowired
    private AuthorMapper authorMapper;

    @Operation(summary = "分页列表")
    @GetMapping("/list")
    public CommonResult<IPage<AuthorResponse>> getList(@ModelAttribute @ParameterObject PageParamRequest pageParamRequest) {
        IPage<AuthorResponse> authorInfoPage = new Page<>(pageParamRequest.getCurrent(), pageParamRequest.getSize());
        IPage<AuthorResponse> authorInfo =  authorService.pageList(authorInfoPage);
        return CommonResult.success(authorInfo);
    }
    @Operation(summary = "添加作者")
    @PostMapping("/add")
    public CommonResult<String> addAuthor(@RequestBody AuthorUpdateRequest request) {
        Author author = authorService.getOne(new QueryWrapper<Author>().eq("name", request.getName()));
        if (author != null) {
            return CommonResult.failed("重复添加标签");
        }
        author = new Author();
        author.setName(request.getName());
        author.setBio(request.getBio());
        author.setAvatar(request.getAvatar());
        boolean save = authorService.save(author);
        return save ? CommonResult.success() : CommonResult.failed();
    }
    @Operation(summary = "修改作者")
    @PutMapping("/update")
    public CommonResult<String> update (@RequestBody @Validated AuthorUpdateRequest request) {
        if (null == request.getId() || request.getId() <= 0) {
            return CommonResult.validateFailed("id不合法");
        }
        LambdaQueryWrapper<Author> authorQueryWrapper = new LambdaQueryWrapper<>();
        authorQueryWrapper.eq(Author::getName, request.getName());
        authorQueryWrapper.ne(Author::getId, request.getId());
        Author author = authorService.getOne(authorQueryWrapper);
        if (author != null) {
            return CommonResult.failed("已有该标签");
        }
        author = new Author();
        author.setId(request.getId());
        author.setName(request.getName());
        author.setBio(request.getBio());
        author.setUpdatedAt(Date.from(LocalDateTime.now().atZone(ZoneId.systemDefault()).toInstant()));
        boolean update = authorService.updateById(author);
        return update ? CommonResult.success() : CommonResult.failed();
    }
    @Operation(summary = "根据id查询作者")
    @GetMapping("/getAuthorById/{id}")
    public CommonResult<Author> getAuthorById(@PathVariable Integer id) {
        if (null == id || id <= 0) {
            return CommonResult.validateFailed("id不合法");
        }
        Author author = authorService.getById(id);
        return CommonResult.success(author);
    }
    @Operation(summary = "根据作品Id查询作者")
    @GetMapping("/getAuthorByAlbumId/{id}")
    public CommonResult<List<AuthorTempVo>> getAuthorByAlbumId(@PathVariable Integer id) {
        if (null == id || id <= 0) {
            return CommonResult.validateFailed("id不合法");
        }
        LambdaQueryWrapper<AlbumAuthor> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AlbumAuthor::getAlbumId,id);
        List<AlbumAuthor> albumAuthorList = albumAuthorService.list(wrapper);
        List<AuthorTempVo> list = albumAuthorList.stream().map(albumAuthor -> authorMapper.getAuthorById(albumAuthor.getAuthorId())).toList();
        return CommonResult.success(list);
    }

}
